﻿using System.Collections.Generic;
using Xenta.Api;
using Xenta.Entities;
using Xenta.Security;
using Xenta.Utils;

namespace Xenta.Operations
{
    #region __General__

    /// <summary>
    /// Creates an account attribute.
    /// </summary>
    public class CreateAccountAttribute : CreateGenericAttribute<AccountAttributeEntity>
    {
    }

    /// <summary>
    /// Creates an account attribute.
    /// </summary>
    public class UpdateAccountAttribute : UpdateGenericAttribute<AccountAttributeEntity>
    {
    }

    /// <summary>
    /// Sets an account attribute.
    /// </summary>
    public class SetAccountAttribute : SetGenericAttribute<AccountAttributeEntity>,
        ISecuredOperation, IApiOperation
    {
        #region Properties

        /// <summary>
        /// Gets the collection of security policies.
        /// </summary>
        public IEnumerable<SecurityPolicy> SecurityPolicies
        {
            get
            {
                yield return new SecurityPolicy()
                    .IsAuthenticated()
                    .HasPermission("ACCNT");
                yield return new SecurityPolicy()
                    .IsAuthenticated()
                    .Rule((ctx, uow) => ctx.Account(uow).EntityID == HolderID);
            }
        }

        #endregion
    }

    /// <summary>
    /// Deletes the account attribute.
    /// </summary>
    public class DeleteAccountAttribute : DeleteGenericAttribute<AccountAttributeEntity>
    {
    }

    /// <summary>
    /// Gets the account attribute by identifier.
    /// </summary>
    public class GetAccountAttribute : GetGenericAttribute<AccountAttributeEntity>
    {
    }

    /// <summary>
    /// Gets the account attribute by the key.
    /// </summary>
    public class GetAccountAttributeByKey : GetGenericAttributeByKey<AccountAttributeEntity>
    {
    }

    /// <summary>
    /// Gets the account attribute value by key.
    /// </summary>
    public class GetAccountAttributeValueByKey : GetGenericAttributeValueByKey<AccountAttributeEntity>,
        IApiOperation
    {
    }

    #endregion

    #region __Bulk__

    /// <summary>
    /// Updates the account attributes from dictionary.
    /// </summary>
    public class BulkUpdateAccountAttributes : BulkUpdateGenericAttributes<AccountAttributeEntity>,
        ISecuredOperation, IApiOperation
    {
        #region Properties

        /// <summary>
        /// Gets the collection of security policies.
        /// </summary>
        public IEnumerable<SecurityPolicy> SecurityPolicies
        {
            get
            {
                yield return new SecurityPolicy()
                    .IsAuthenticated()
                    .HasPermission("ACCNT");
                yield return new SecurityPolicy()
                    .IsAuthenticated()
                    .Rule((ctx, uow) => ctx.Account(uow).EntityID == HolderID);
            }
        }

        #endregion
    }

    #endregion
}
